public class Solution2024 {
    public int maxConsecutiveAnswers(String answerKey, int k) {
        int l=0,r=answerKey.length();
        int ans=0;
        while (l<=r){
            int mid=(l+r)/2;
            int cur=0;
            boolean flag=false;
            for (int i=0;i<mid;i++){
                if (answerKey.charAt(i)=='T'){
                    cur++;
                }
            }
            if (cur<=k || mid-cur<=k){
                flag=true;
            }
            for (int i=mid;i<answerKey.length();i++){
                if (answerKey.charAt(i)=='T'){
                    cur++;
                }
                if (answerKey.charAt(i-mid)=='T'){
                    cur--;
                }
                if (cur<=k || mid-cur<=k){
                    flag=true;
                }
            }
            if (flag){
                ans=mid;
                l=mid+1;
            }else {
                r=mid-1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2024().maxConsecutiveAnswers("TFFT",1));
    }
}
